package com.erification.web.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.erification.web.model.Address;
import com.erification.web.model.Celephone;
import com.erification.web.model.Email;
import com.erification.web.model.Group;
import com.erification.web.model.TrueName;
import com.erification.web.model.User;
import com.erification.web.service.AddressService;
import com.erification.web.service.CelephoneService;
import com.erification.web.service.EmailService;
import com.erification.web.service.GroupService;
import com.erification.web.service.TrueNameService;
import com.erification.web.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller("modifyAction")
public class ModifyAction extends ActionSupport {
	@Resource 
	private UserService userService;
	@Resource
	private AddressService addressService;
	@Resource
	private CelephoneService celephoneService;
	@Resource
	private EmailService emailService;
	@Resource
	private TrueNameService trueNameService;
	@Resource
	private GroupService groupService;
	
	private TrueName trueName;
	private Address address;
	private Email email;
	private Celephone celephone;
	private String index;
	private String id;
	private String nameId;
	private String addressId;
	private String emailId;
	private String celephoneId;
	
	public String getNameId() {
		return nameId;
	}
	public void setNameId(String nameId) {
		this.nameId = nameId;
	}
	public String getAddressId() {
		return addressId;
	}
	public void setAddressId(String addressId) {
		this.addressId = addressId;
	}
	public String getEmailId() {
		return emailId;
	}
	public void setEmailId(String emailId) {
		this.emailId = emailId;
	}
	public String getCelephoneId() {
		return celephoneId;
	}
	public void setCelephoneId(String celephoneId) {
		this.celephoneId = celephoneId;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getIndex() {
		return index;
	}
	public void setIndex(String index) {
		this.index = index;
	}
	public TrueName getTrueName() {
		return trueName;
	}
	public void setTrueName(TrueName trueName) {
		this.trueName = trueName;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public Email getEmail() {
		return email;
	}
	public void setEmail(Email email) {
		this.email = email;
	}
	public Celephone getCelephone() {
		return celephone;
	}
	public void setCelephone(Celephone celephone) {
		this.celephone = celephone;
	}
	
	public String modifyTrueName() throws Exception {
		String username = (String) ActionContext.getContext().getSession().get("username");
		User user = userService.getUser(username);
		long user_id = user.getId();
		List list = (List) ActionContext.getContext().getSession().get("truename");
		TrueName beforeModify = (TrueName) list.get(Integer.parseInt(index));	//获得要修改的truename;
		trueNameService.ModifyFirstName(user_id, beforeModify, trueName.getFirstName());
		trueNameService.ModifyLastName(user_id, beforeModify, trueName.getLastName());
		beforeModify.setFirstName(trueName.getFirstName());
		beforeModify.setLastName(trueName.getLastName());
		return SUCCESS;
	}
	
	public String modifyCelephone() throws Exception {
		String username = (String) ActionContext.getContext().getSession().get("username");
		User user = userService.getUser(username);
		long user_id = user.getId();
		List list = (List) ActionContext.getContext().getSession().get("celephone");
		Celephone beforeModify = (Celephone) list.get(Integer.parseInt(index));	//获得要修改的celephone;
		celephoneService.modifyCelephone(user_id, beforeModify, celephone.getCelephone());
		beforeModify.setCelephone(celephone.getCelephone());
		return SUCCESS;
	}
	
	public String modifyAddress() throws Exception {
		String username = (String) ActionContext.getContext().getSession().get("username");
		User user = userService.getUser(username);
		long user_id = user.getId();
		List list = (List) ActionContext.getContext().getSession().get("address");
		Address beforeModify = (Address) list.get(Integer.parseInt(index));	//获得要修改的address;
		addressService.modifyAddress(user_id, beforeModify, address.getAddress());
		beforeModify.setAddress(address.getAddress());
		return SUCCESS;
	}

	public String modifyEmail() throws Exception {
		String username = (String) ActionContext.getContext().getSession().get("username");
		User user = userService.getUser(username);
		long user_id = user.getId();
		List list = (List) ActionContext.getContext().getSession().get("email");
		Email beforeModify = (Email) list.get(Integer.parseInt(index));	//获得要修改的address;
		emailService.modifyEmail(user_id, beforeModify, email.getEmail());
		beforeModify.setEmail(email.getEmail());
		return SUCCESS;
	}
	
	public String modifyGroup() throws Exception {
		String username =  (String) ActionContext.getContext().getSession().get("username");
		User user = userService.getUser(username);
		List list = (List) ActionContext.getContext().getSession().get("group");
		Group newGroup = (Group) list.get(Integer.parseInt(index));
		list.remove(Integer.parseInt(index));
		newGroup.setUser(user);
		newGroup.setCelephone(celephoneService.get(Long.parseLong(celephoneId)));
		newGroup.setAddress(addressService.get(Long.parseLong(addressId)));
		newGroup.setTrueName(trueNameService.get(Long.parseLong(nameId)));
		newGroup.setEmail(emailService.get(Long.parseLong(emailId)));
		groupService.modifyGroup(Long.parseLong(id), newGroup);
		list.add(newGroup);
		ActionContext.getContext().getSession().put("group", list);
		return "group";
	}
}
